from fastapi import APIRouter, status
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi import Request

from app.controllers.weather_controllers import (AddWeatherToRegion,
                                                 CreateWeatherController,
                                                 DeleteWeather,
                                                 DeleteWeatherForRegion,
                                                 EditWeatherController,
                                                 GetWeatherById)
from app.schemas.weather_schemas import (CreateWeatherRequestSchema,
                                         EditWeatherRequest,
                                         EditWeatherRequestSchema,
                                         GetWeatherForRegion,
                                         GetWeatherResponseSchema,
                                         RegionDeleteWeatherResponse)

weather_router = APIRouter(prefix="/region", tags=["Weather routers"])

templates = Jinja2Templates(directory="templates")


@weather_router.post("/weather", status_code=status.HTTP_200_OK)
async def add_weather(request: CreateWeatherRequestSchema) -> GetWeatherResponseSchema:
    controller = CreateWeatherController(request=request)
    return await controller.execute()


@weather_router.get("/weather/weather-front", response_class=HTMLResponse)
async def weather_front(request: Request):
    return templates.TemplateResponse(
        request=request, name="weather.html", context={"id": 1}
    )


@weather_router.get("/weather/{regionId}", status_code=status.HTTP_200_OK)
async def get_weather_by_id(regionId: int) -> GetWeatherForRegion:
    controller = GetWeatherById(region_id=regionId)
    return await controller.execute()


@weather_router.put("/weather/{regionId}", status_code=status.HTTP_200_OK)
async def edit_weather(region_id: int, request: EditWeatherRequest):
    controller = EditWeatherController(region_id=region_id, request=request)
    return await controller.execute()


@weather_router.delete("/weather/{regionId}", status_code=status.HTTP_200_OK)
async def delete_weather(regionId: int):
    controller = DeleteWeather(region_id=regionId)
    return await controller.execute()


@weather_router.post("/{regionId}/weather/{weatherId}", status_code=status.HTTP_200_OK)
async def add_weather_for_region(regionId: int, weatherId: int) -> GetWeatherForRegion:
    controller = AddWeatherToRegion(region_id=regionId, weather_id=weatherId)
    return await controller.execute()


@weather_router.delete(
    "/{regionId}/weather/{weatherId}", status_code=status.HTTP_200_OK
)
async def delete_weather_for_region(
        regionId: int, weatherId: int
) -> RegionDeleteWeatherResponse:
    controller = DeleteWeatherForRegion(region_id=regionId, weather_id=weatherId)
    return await controller.execute()
